package com.laobiao.file_netty_lib.packet;

import com.laobiao.file_netty_lib.exception.FileBaseException;

/**
 * 基础包
 * @author 38079
 *
 */
public abstract class BasePacket {
	/**
	 * 包类型
	 */
	private PacketType packetType;

	/**
	 * 业务组编号
	 */
	private int businessGroup;
	
	public int getBusinessGroup() {
		return businessGroup;
	}

	public void setBusinessGroup(int businessGroup) {
		if(businessGroup<0) {
			throw new FileBaseException("业务组编号不饿能小于0！", FileBaseException.ERR_UNPREDICTABLE);
		}
		this.businessGroup = businessGroup;
	}

	public PacketType getPacketType() {
		return packetType;
	}

	/**
	 * 包类型
	 * @param packetType
	 */
	public void setPacketType(PacketType packetType) {
		if(packetType==null) {
			throw new FileBaseException("包类型不能为空！", FileBaseException.ERR_PARAM_NULL);
		}
		this.packetType = packetType;
	}
	
	/**
	 * 判断字节数组的包类型，只取第一位
	 * @param packetBytes
	 * @return
	 */
	public static PacketType judgePacketType(byte[] packetBytes) {
		int index=(int)packetBytes[0];
		if(index==PacketType.COMMAND.getType()) {
			return PacketType.COMMAND;
		}else if(index==PacketType.DATA.getType()) {
			return PacketType.DATA;
		}
		throw new FileBaseException("数据错误，无法读取包类型", FileBaseException.ERR_PARAM_WRONG);
	}
	
	/**
	 * 取命令字节数组，长度必须满足1024
	 * @return
	 */
	public abstract byte[] getPacketBytes();
	
}
